<?php
/**
 * Created by PhpStorm.
 * User: wuz
 * Date: 2016/8/30
 * Time: 21:23
 */

namespace Ycf\Lession\MysqlClass;

class MysqlClass
{
    static $host     = "localhost";
    static $userName = "root";
    static $password = "123456";
    static $dbName   = "test_db";
    private $link    = null;
    public function _construct($host, $userName, $password, $dbName)
    {
        MysqlClass::$host     = $host;
        MysqlClass::$userName = $userName;
        MysqlClass::$password = $password;
        MysqlClass::$dbName   = $dbName;
        MysqlClass::connnect($host, $userName, $password, $dbName);
    }
    /*
    数据库连接
     */
    public function connnect($host, $usrName, $passord, $dbName)
    {
        $link    = mysqli_connect($host, $usrName, $passord, $dbName);
        $charset = "utf8";
        mysqli_set_charset($link, $charset); //设置字符集
        if ($link == false) {
            echo 'false';
        }
        //数据库连接状态
        $this->link = $link;

    }
    /*
    数据查询
     */
    public function fetchOne($sql)
    {

        $this->link;
        $result = mysqli_query($this->link, $sql);
        return mysqli_fetch_assoc($result);
    }
    /*
    数据删除
     */
    public function delete($table, $where = null)
    {
        $where = $where == null ? '' : ' WHERE ' . $where;
        $sql   = "DELETE FROM {$table}{$where}";
        mysqli_query($this->link, $sql);
        $mysqli = $this->link;
        return $mysqli->affected_rows; //返回受影响的行数
    }
}
$mysql = new MysqlClass();
$mysql->connnect("localhost", "root", "123456", "test_db");
$sql = "select * from student";
echo "<pre>";
print_r($mysql->fetchOne($sql));
$delectSql = "id = 2";
$mysql->delete("student", $delectSql);
